<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,Authorization, Testpc,App-Version");
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Content-type: text/html; charset=utf-8");

function httpPost($url, $data) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    if(stripos($url,'https') === 0) {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    if($response === false) {
        echo 'Curl error: ' . curl_error($curl);
    }
    curl_close($curl);
    return $response;
}

if(!empty($_GET['type'])) {
    if(in_array($_GET['type'], array('initCustomer', 'drawnLottery', 'submitAddress', 'getGiftCustomerList'))) {
    echo httpPost('http://hbweixin.twomi.cn/api_hb_bluemember/'.$_GET['type'],$_POST);
    } else if($_GET['type'] == 'getWeixinUser') {
        echo (httpPost('https://www.bluemembers.com.cn/weixin/WxUserBmUser/GetBindUserInfo?openId='.$_GET['openid'],array()));
    } else if($_GET['type'] == 'bindWeixinUser') {
        echo httpPost('https://www.bluemembers.com.cn/weixin/WxUserBmUser/BindUser?openId='.$_GET['openid'].'&userId='.$_GET['userId'],array());
    } else if($_GET['type'] == 'wx') {
        header('Content-Type: application/javascript');
        //$url = urlencode("https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
        $url = empty($_SERVER['HTTP_REFERER']) ? '' : urlencode($_SERVER['HTTP_REFERER']);
        echo file_get_contents('http://hbweixin.twomi.cn/2016/accessToken/signPackage.php?url='.$url);
    } else if($_GET['type'] == 'test') {
        echo 'hello world!!!';
    } else  {
        header('Content-Type: application/javascript');
        echo httpPost('http://hbweixin.twomi.cn/api_hb_bluemember/getToken',array());
    }
} else {
    header('Content-Type: application/javascript');
    echo httpPost('http://hbweixin.twomi.cn/api_hb_bluemember/getToken',array());
}


